{% extends 'form.html' %}

{% block other_buttons %}
<button id="send_code" class="btn btn-primary">发送验证码</button>
{% endblock %}

{% block script_extends %}
<script type="text/javascript">
    $("#send_code").click(function () {
        var email = $("#id_email").val();
        if (email == '') {
            $('#tip').text("* 邮箱不能为空");
            return false;
        }
        // 发送验证码
        $.ajax({
            url: "{% url 'send_verification_code' %}",
            type: "GET",
            data: {
                'email': email,
                'send_for': 'forgot_password_code',
            },
            cache: false,
            success: function (data) {
                if (data['data'] == 'ERROR') {
                    alert(data['status']);
                }
            }
        });
        // 把按钮变灰
        $(this).addClass('disabled');
        $(this).attr('disabled', true);
        var time = 30;
        $(this).text(time + 's');
        var intervar = setInterval(() => {
            if (time <= 0) {
                clearInterval(intervar);
                $(this).removeClass('disabled');
                $(this).attr('disabled', false);
                $(this).text('发送验证码');
                return false;
            }
            time--;
            $(this).text(time + 's');
        }, 1000);
    });
</script>
{% endblock %}
